package Endpoint

import (
	"context"
	"fmt"
	"github.com/go-kit/kit/endpoint"
	Services2 "go-kit/demo2/Services"
)

//定义请求参数结构
type UserRequest struct {
	Uid    int    `json:Uid`
	Method string `json:Method`
}

//定义返回参数结构
type UserResponse struct {
	Result string `json:"result"`
}

//我理解为controller 调用 逻辑层
func GetUserEndPoint(UserService Services2.IGetUserService) endpoint.Endpoint {

	return func(ctx context.Context, request interface{}) (response interface{}, err error) {

		//断言 理解 利用结构 拿到参数 request 就不用理解了 可能是一个 请求对象
		r := request.(UserRequest)

		fmt.Println(r.Method)

		result := "非法操作模式"

		//根据 REST 定义类型
		switch {
		//查询
		case r.Method == "GET":
			result = UserService.GetUserName(r.Uid)
		//写入
		case r.Method == "POST":
			result = UserService.AddUserName(r.Uid)
		//更新
		case r.Method == "UPDATE":
			result = UserService.EditUserName(r.Uid)
		//删除
		case r.Method == "DELETE":
			result = UserService.DelUserName(r.Uid)
		}

		//返回数据
		return UserResponse{Result: result}, nil

	}

}
